﻿using CPPEI.Net;
using CPPEI.ShareScreen.Contract;
using CPPEI.ShareScreen.Event.NCL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ShareScreen.ServerProxy
{
    public class ServerProxyClient
    {
        private CPPEI.Net.TCP.TCPClient _client;
        private byte[] _buffer;

        public ServerProxyClient(CPPEI.Net.TCP.TCPClient client)
        {
            _client = client;
            _buffer = new byte[4000];
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveNCLGetClientListEvent(Event.NCL.NCLGetClientListEvent @event)
        {
            if (@event.State != CPPEI.Event.NCL.NCLEventState.Sending)
            {
                return;
            }
            CPPEI.ShareScreen.Contract.ReqGetClientListData requestData = new Contract.ReqGetClientListData();
            requestData.Start = 0;
            requestData.Limit = 25;

            NetPackage netPackage = new NetPackage(Contract.Command.GetClientList, NetReplayMode.Self, 0, Serializer.ToBinary(requestData));
            _client.Send(netPackage);
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveNCLRDConnectToClientEvent(Event.NCL.NCLRDConnectToClientEvent @event)
        {
            if (@event.State != CPPEI.Event.NCL.NCLEventState.Sending)
            {
                return;
            }

            Contract.ReqRDConnectToClientData requestData = new ReqRDConnectToClientData();
            requestData.TargetClientID = @event.TargetClientID;

            NetPackage netPackage = new NetPackage(Contract.Command.RDConnectToClient, NetReplayMode.Self, 0, Serializer.ToBinary(requestData));
            _client.Send(netPackage);
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveNCLUploadDesktopEvent(NCLUploadDesktopEvent @event)
        {
            if (@event.State != CPPEI.Event.NCL.NCLEventState.Sending)
            {
                return;
            }

            Contract.ReqRDData requestData = new  ReqRDData();
            requestData.Data = @event.Data;

            NetPackage netPackage = new NetPackage(Contract.Command.RD, NetReplayMode.Room_Other, 0, Serializer.ToBinary(requestData));
            _client.Send(netPackage);
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveNCLRDDisconnectEvent(NCLRDDisconnectEvent @event)
        {
            if (@event.State != CPPEI.Event.NCL.NCLEventState.Sending)
            {
                return;
            }

            Contract.ReqRDDisconnectData requestData = new ReqRDDisconnectData();
            requestData.TargetClientID = @event.TargetClientID;
            requestData.RoomID = @event.RoomID;

            NetPackage netPackage = new NetPackage(Contract.Command.RDDisconnect, NetReplayMode.Self, 0, Serializer.ToBinary(requestData));
            _client.Send(netPackage);
        }
    }
}
